package com.guli.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.service.AttrService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * 商品属性
 *
 * @author yaodao
 * @email sunlightcs@gmail.com
 * @date 2022-05-23 16:52:32
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @SuppressWarnings("all")
    @Autowired
    private AttrGroupDao attrGroupDao;

    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{catelogId}")
    public R list(@PathVariable("type") String type, @PathVariable("catelogId") Long catelogId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(type, catelogId, params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrEntity attr = attrService.getById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 查询某个分组下的规格参数
     *
     * @param attrGroupId
     * @return
     */
    //attr/relation
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> list = attrService.attrRelation(attrGroupId);
        return R.ok().put("data", list);
    }

    /**
     * 查询没有关联任何分组的规格参数
     *
     * @param params
     * @return
     */
    //    noattr/relation
    @RequestMapping("/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.noattrRelation(params);
        return R.ok().put("page", page);
    }

    //    attr/relation
    @RequestMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrEntity> attr) {
        attrService.updateBatchById(attr);
        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrEntity attr) {
        attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrEntity attr) {
        if (attr.getAttrGroupId() != null) {
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attr.getAttrGroupId());
            attr.setAttrGroupName(attrGroupEntity.getAttrGroupName());
        }
        attrService.updateById(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

}
